//
//  AboutAppViewController.m
//  project
//
//  Created by 圆圆陈 on 2017/12/16.
//  Copyright © 2017年 黄建华. All rights reserved.
//

#import "AboutAppViewController.h"

@interface AboutAppViewController ()<UITableViewDelegate, UITableViewDataSource>
@property(nonatomic, strong) UIImageView *backImageView;
@property(nonatomic, strong) UITableView *tableView;
@property(nonatomic, strong) NSArray     *titleArr;
@property(nonatomic, strong) UILabel     *currentV;
@property(nonatomic, strong) UILabel     *latestV;
@property(nonatomic, strong) UIButton    *autoUpdateBtn;
@property(nonatomic, strong) ZJSwitch    *autoSynchronousBtn;
@property(nonatomic, strong) UIButton    *distanceGBtn;
@property(nonatomic, strong) UIButton    *distanceYBtn;
@property(nonatomic, strong) UIButton    *temperatureCBtn;
@property(nonatomic, strong) UIButton    *temperatureHBtn;
@property(nonatomic, strong) UIButton    *kilogramBtn;
@property(nonatomic, strong) UIButton    *poundBtn;
@property(nonatomic, strong) UIButton    *eTimeBtn;
@property(nonatomic, strong) UIButton    *zTimeBtn;
@end

@implementation AboutAppViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    UIImage *backImage = UIImageNamed(@"背景");
    _backImageView                        = [[UIImageView alloc]initWithFrame:CGRectMake(0, 20+FitSimpleIpx(0, 20), HHHWIDTH, HHHWIDTH*backImage.size.height/backImage.size.width)];
    _backImageView.userInteractionEnabled = YES;
    _backImageView.image                  = backImage;
    
    UIButton *backBtn = [[UIButton alloc]initWithFrame:CGRectMake(5, 0, 44, 44)];
    [backBtn setImageNormal:@"进入白"];
    [backBtn addTouchUpTarget:self action:@selector(backToLastVC)];
    [_backImageView addSubview:backBtn];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake((HHHWIDTH-140)/2, 0, 140, 44)];
    titleLabel.text          = SN_Text(@"关于APP");
    titleLabel.font          = SNFONT(17);
    titleLabel.textColor     = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [_backImageView addSubview:titleLabel];
    
    _tableView                 = [[UITableView alloc]initWithFrame:CGRectMake(0, 20+FitSimpleIpx(0, 20), HHHWIDTH, HHHHEIGHT-20-FitSimpleIpx(0, 20)-FitSimpleIpx(0, 34)) style:UITableViewStylePlain];
    _tableView.delegate        = self;
    _tableView.dataSource      = self;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.separatorStyle  = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
    UIView   *footerBackView = [[UIView alloc]initWithFrame:CGRectMake(0, HHHHEIGHT-FitH(60), HHHWIDTH, FitH(60))];
    UIButton *checkBtn       = [UIButton buttonWithType:UIButtonTypeCustom];
    checkBtn.frame = CGRectMake(HHHWIDTH/10, FitH(10), HHHWIDTH*4/5, FitH(40));
    [checkBtn setBackgroundColor:[UIColor blackColor]];
    [checkBtn setTitleNormal:HHH_Text(@"检查更新")];
    checkBtn.titleLabel.font = SNFONT(FitFont(20));
    [checkBtn setTitleColorNormal:[UIColor whiteColor]];
    checkBtn.layer.cornerRadius = 5;
    checkBtn.clipsToBounds      = YES;
    [checkBtn addTouchUpTarget:self action:@selector(checkUpdate)];
    //    [footerBackView addSubview: checkBtn];
    
    [SN_Net getDeviceSFromServerWithSuccess:^(id object) {
        if (SN_USER.isSupportSettingTimeUnit == YES) {
            _titleArr                  = @[HHH_Text(@"APP当前版本"), HHH_Text(@"APP最新版本"), HHH_Text(@"自动同步"), HHH_Text(@"距离显示格式"), HHH_Text(@"温度显示格式"), HHH_Text(@"重量显示格式"),HHH_Text(@"手环时间制式")];
        } else {
            _titleArr                  = @[HHH_Text(@"APP当前版本"), HHH_Text(@"APP最新版本"), HHH_Text(@"自动同步"), HHH_Text(@"距离显示格式"), HHH_Text(@"温度显示格式"), HHH_Text(@"重量显示格式")];
        }
        [_tableView reloadData];
    } fail:^(id object) {
        
    }];
    
    _tableView.tableHeaderView = _backImageView;
    _tableView.tableFooterView = footerBackView;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    DEF_WEAKSELF_(AboutAppViewController)
    [SN_Net getAppUpdateInfoFromServerWithSuccess:^(id object) {
        [weakSelf updateLastVersion];
    } fail:^(id object) {
    }];
}

- (void)updateLastVersion
{
    _latestV.text = [NSString stringWithFormat:@"V %@", SN_USER.lastOnlineAppVersion?SN_USER.lastOnlineAppVersion:@"1.0.0"];
}

- (void)backToLastVC {
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _titleArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    //    return (_tableView.height-_backImageView.height-FitH(60))/6;
    return 1.0*(_tableView.height - _backImageView.height) *0.75/6.0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (!cell) {
        cell                = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.textLabel.text = _titleArr[indexPath.row];
    cell.textLabel.font = SNFONT(16.0);
    switch (indexPath.row) {
        case 0:
            _currentV               = [[UILabel alloc]initWithFrame:CGRectMake(HHHWIDTH-FitW(90)-20, FitH(19), FitW(80), FitH(23.5))];
            _currentV.font          = SNFONTMIN(16.0);
            _currentV.textColor     = [UIColor blackColor];
            _currentV.textAlignment = NSTextAlignmentRight;
            _currentV.text          = [NSString stringWithFormat:@"V %@", SN_USER.currentAppVersion];
            [cell addSubview:_currentV];
            break;
        case 1:
            _latestV               = [[UILabel alloc]initWithFrame:CGRectMake(HHHWIDTH-FitW(90)-20, FitH(19), FitW(80), FitH(23.5))];
            _latestV.font          = SNFONTMIN(16.0);
            _latestV.textColor     = [UIColor blackColor];
            _latestV.textAlignment = NSTextAlignmentRight;
            [cell addSubview:_latestV];
            [self updateLastVersion];
            break;
        case 2:
            
            _autoSynchronousBtn     = [[ZJSwitch alloc] initWithFrame:CGRectMake(_latestV.right -50, FitH(10), 50, 22)];
            _autoSynchronousBtn.tag = 101;
            _autoSynchronousBtn.on  = SN_USER.autoUpdateData;
            [_autoSynchronousBtn addTarget:self action:@selector(handleSwitchEvent:) forControlEvents:UIControlEventValueChanged];
            [cell addSubview:_autoSynchronousBtn];
            break;
        case 3:
            _distanceGBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [_distanceGBtn setImageNormal:@"未选中"];
            [_distanceGBtn setImageSelecte:@"选中1"];
            [_distanceGBtn setTitleNormal:@"km"];
            [_distanceGBtn setTitleColorNormal:[UIColor blackColor]];
            _distanceGBtn.titleLabel.font            = SNFONTMIN(16.0);
            _distanceGBtn.frame                      = CGRectMake(HHHWIDTH*1/3+FitW(75) +5, FitH(19), FitW(70), FitH(23.5));
            _distanceGBtn.tag                        = 102;
            _distanceGBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
            [_distanceGBtn addTouchUpTarget:self action:@selector(btnClick:)];
            [cell addSubview:_distanceGBtn];
            _distanceGBtn.selected = !SN_USER.isMetricSystem;
            
            _distanceYBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [_distanceYBtn setImageNormal:@"未选中"];
            [_distanceYBtn setImageSelecte:@"选中1"];
            [_distanceYBtn setTitleNormal:@"mile"];
            [_distanceYBtn setTitleColorNormal:[UIColor blackColor]];
            _distanceYBtn.titleLabel.font            = SNFONTMIN(16.0);
            _distanceYBtn.frame                      = CGRectMake(_distanceGBtn.right+FitW(40) -5, FitH(19), FitW(70), FitH(23.5));
            _distanceYBtn.tag                        = 103;
            _distanceYBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
            [_distanceYBtn addTouchUpTarget:self action:@selector(btnClick:)];
            [cell addSubview:_distanceYBtn];
            _distanceYBtn.selected = SN_USER.isMetricSystem;
            break;
        case 4:
            _temperatureCBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [_temperatureCBtn setImageNormal:@"未选中"];
            [_temperatureCBtn setImageSelecte:@"选中1"];
            [_temperatureCBtn setTitleNormal:@"℃"];
            [_temperatureCBtn setTitleColorNormal:[UIColor blackColor]];
            _temperatureCBtn.titleLabel.font            = SNFONTMIN(16.0);
            _temperatureCBtn.frame                      = CGRectMake(HHHWIDTH*1/3+FitW(75) +5, FitH(19), FitW(70), FitH(23.5));
            _temperatureCBtn.tag                        = 104;
            _temperatureCBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
            [_temperatureCBtn addTouchUpTarget:self action:@selector(btnClick:)];
            [cell addSubview:_temperatureCBtn];
            _temperatureCBtn.selected = !SN_USER.temperatureSet;
            
            _temperatureHBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [_temperatureHBtn setImageNormal:@"未选中"];
            [_temperatureHBtn setImageSelecte:@"选中1"];
            [_temperatureHBtn setTitleNormal:@"℉"];
            [_temperatureHBtn setTitleColorNormal:[UIColor blackColor]];
            _temperatureHBtn.titleLabel.font            = SNFONTMIN(16.0);
            _temperatureHBtn.frame                      = CGRectMake(_temperatureCBtn.right+FitW(40)-5, FitH(19), FitW(70), FitH(23.5));
            _temperatureHBtn.tag                        = 105;
            _temperatureHBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
            [_temperatureHBtn addTouchUpTarget:self action:@selector(btnClick:)];
            [cell addSubview:_temperatureHBtn];
            _temperatureHBtn.selected = SN_USER.temperatureSet;
            break;
        case 5:
            _kilogramBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [_kilogramBtn setImageNormal:@"未选中"];
            [_kilogramBtn setImageSelecte:@"选中1"];
            [_kilogramBtn setTitleNormal:@"kg"];
            [_kilogramBtn setTitleColorNormal:[UIColor blackColor]];
            _kilogramBtn.titleLabel.font            = SNFONTMIN(16.0);
            _kilogramBtn.frame                      = CGRectMake(HHHWIDTH*1/3+FitW(75)+5, FitH(19), FitW(70), FitH(23.5));
            _kilogramBtn.tag                        = 106;
            _kilogramBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
            [_kilogramBtn addTouchUpTarget:self action:@selector(btnClick:)];
            [cell addSubview:_kilogramBtn];
            _kilogramBtn.selected = !SN_USER.weightunitSet;
            
            _poundBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [_poundBtn setImageNormal:@"未选中"];
            [_poundBtn setImageSelecte:@"选中1"];
            [_poundBtn setTitleNormal:@"lb"];
            [_poundBtn setTitleColorNormal:[UIColor blackColor]];
            _poundBtn.titleLabel.font            = SNFONTMIN(16.0);
            _poundBtn.frame                      = CGRectMake(_kilogramBtn.right+FitW(40)-5, FitH(19), FitW(70), FitH(23.5));
            _poundBtn.tag                        = 107;
            _poundBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
            [_poundBtn addTouchUpTarget:self action:@selector(btnClick:)];
            [cell addSubview:_poundBtn];
            _poundBtn.selected = SN_USER.weightunitSet;
            break;
        case 6:
            _eTimeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [_eTimeBtn setImageNormal:@"未选中"];
            [_eTimeBtn setImageSelecte:@"选中1"];
            [_eTimeBtn setTitleNormal:@"24"];
            [_eTimeBtn setTitleColorNormal:[UIColor blackColor]];
            _eTimeBtn.titleLabel.font            = SNFONTMIN(16.0);
            _eTimeBtn.frame                      = CGRectMake(HHHWIDTH*1/3+FitW(75)+5, FitH(19), FitW(70), FitH(23.5));
            _eTimeBtn.tag                        = 108;
            _eTimeBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
            [_eTimeBtn addTouchUpTarget:self action:@selector(btnClick:)];
            [cell addSubview:_eTimeBtn];
            _eTimeBtn.selected = !SN_USER.is12Time;
            
            _zTimeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [_zTimeBtn setImageNormal:@"未选中"];
            [_zTimeBtn setImageSelecte:@"选中1"];
            [_zTimeBtn setTitleNormal:@"12"];
            [_zTimeBtn setTitleColorNormal:[UIColor blackColor]];
            _zTimeBtn.titleLabel.font            = SNFONTMIN(16.0);
            _zTimeBtn.frame                      = CGRectMake(_kilogramBtn.right+FitW(40)-5, FitH(19), FitW(70), FitH(23.5));
            _zTimeBtn.tag                        = 109;
            _zTimeBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
            [_zTimeBtn addTouchUpTarget:self action:@selector(btnClick:)];
            [cell addSubview:_zTimeBtn];
            _zTimeBtn.selected = SN_USER.is12Time;
            break;
        default:
            break;
    }
    return cell;
}

- (void)checkUpdate {
}

- (void)handleSwitchEvent:(ZJSwitch *)sender {
    SN_USER.autoUpdateData = sender.on;
    [SN_USER updateToDB];
}

- (void)btnClick:(UIButton *)sender {
    switch (sender.tag) {
        case 100:
            sender.selected = !sender.selected;
            break;
        case 101:
            sender.selected        = !sender.selected;
            SN_USER.autoUpdateData = sender.selected;
            break;
        case 102:
            if (sender.selected == NO) {
                sender.selected        = YES;
                _distanceYBtn.selected = NO;
                SN_USER.isMetricSystem = NO;
                [SN_EveryDayModelHelp startUpdateUserInfo];
            }
            break;
        case 103:
            if (sender.selected == NO) {
                sender.selected        = YES;
                _distanceGBtn.selected = NO;
                SN_USER.isMetricSystem = YES;
                [SN_EveryDayModelHelp startUpdateUserInfo];
            }
            break;
        case 104:
            if (sender.selected == NO) {
                sender.selected           = YES;
                _temperatureHBtn.selected = NO;
                SN_USER.temperatureSet    = NO;
                [SN_EveryDayModelHelp startUpdateUserInfo];
            }
            break;
        case 105:
            if (sender.selected == NO) {
                sender.selected           = YES;
                _temperatureCBtn.selected = NO;
                SN_USER.temperatureSet    = YES;
                [SN_EveryDayModelHelp startUpdateUserInfo];
            }
            break;
        case 106:
            if (sender.selected == NO) {
                sender.selected       = YES;
                _poundBtn.selected    = NO;
                SN_USER.weightunitSet = NO;
            }
            break;
        case 107:
            if (sender.selected == NO) {
                sender.selected       = YES;
                _kilogramBtn.selected = NO;
                SN_USER.weightunitSet = YES;
            }
            break;
        case 108:
            if (sender.selected == NO) {
                sender.selected       = YES;
                _zTimeBtn.selected = NO;
                SN_USER.is12Time = NO;
                [SN_EveryDayModelHelp startUpdateUserInfo];
            }
            break;
        case 109:
            if (sender.selected == NO) {
                sender.selected       = YES;
                _eTimeBtn.selected = NO;
                SN_USER.is12Time = YES;
                [SN_EveryDayModelHelp startUpdateUserInfo];
            }
            break;
        default:
            break;
    }
    [SN_USER updateToDB];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

